﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// Pipeline.xaml 的交互逻辑
    /// </summary>
    public partial class Pipeline : UserControl
    {
        private DoubleAnimation flowAnimation;

        public Pipeline()
        {
            InitializeComponent();
            InitializeFlowAnimation();
        }

        /// <summary>
        /// 管道长度
        /// </summary>
        public double PipeLength
        {
            get { return (double)GetValue(PipeLengthProperty); }
            set { SetValue(PipeLengthProperty, value); }
        }

        public static readonly DependencyProperty PipeLengthProperty =
            DependencyProperty.Register("PipeLength", typeof(double), typeof(Pipeline),
                new PropertyMetadata(200.0, new PropertyChangedCallback(PipeLengthChangedCallback)));

        private static void PipeLengthChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Pipeline)d;
            if (control == null) return;

            if (e.NewValue is double length)
            {
                control.UpdatePipeLength(length);
            }
        }

        /// <summary>
        /// 是否显示流体
        /// </summary>
        public bool ShowFluid
        {
            get { return (bool)GetValue(ShowFluidProperty); }
            set { SetValue(ShowFluidProperty, value); }
        }

        public static readonly DependencyProperty ShowFluidProperty =
            DependencyProperty.Register("ShowFluid", typeof(bool), typeof(Pipeline),
                new PropertyMetadata(false, new PropertyChangedCallback(ShowFluidChangedCallback)));

        private static void ShowFluidChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Pipeline)d;
            if (control == null) return;

            if (e.NewValue is bool showFluid)
            {
                control._fluidIndicator.Visibility = showFluid ? Visibility.Visible : Visibility.Collapsed;
                control._flowArrow.Visibility = showFluid ? Visibility.Visible : Visibility.Collapsed;

                if (showFluid)
                {
                    control.StartFlowAnimation();
                }
                else
                {
                    control.StopFlowAnimation();
                }
            }
        }

        /// <summary>
        /// 流体颜色
        /// </summary>
        public Brush FluidColor
        {
            get { return (Brush)GetValue(FluidColorProperty); }
            set { SetValue(FluidColorProperty, value); }
        }

        public static readonly DependencyProperty FluidColorProperty =
            DependencyProperty.Register("FluidColor", typeof(Brush), typeof(Pipeline),
                new PropertyMetadata(new SolidColorBrush(Color.FromRgb(64, 128, 255)), 
                    new PropertyChangedCallback(FluidColorChangedCallback)));

        private static void FluidColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Pipeline)d;
            if (control == null) return;

            if (e.NewValue is Brush brush)
            {
                control._fluidIndicator.Fill = brush;
            }
        }

        /// <summary>
        /// 管道方向（水平/垂直）
        /// </summary>
        public Orientation PipeOrientation
        {
            get { return (Orientation)GetValue(PipeOrientationProperty); }
            set { SetValue(PipeOrientationProperty, value); }
        }

        public static readonly DependencyProperty PipeOrientationProperty =
            DependencyProperty.Register("PipeOrientation", typeof(Orientation), typeof(Pipeline),
                new PropertyMetadata(Orientation.Horizontal, new PropertyChangedCallback(OrientationChangedCallback)));

        private static void OrientationChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Pipeline)d;
            if (control == null) return;

            if (e.NewValue is Orientation orientation)
            {
                control.UpdateOrientation(orientation);
            }
        }

        private void UpdatePipeLength(double length)
        {
            // 更新主管道长度 (管道本体长度 = 总长度 - 左右接口宽度)
            double pipeBodyLength = length - 40; // 左右接口各占20px
            _mainPipe.Width = pipeBodyLength;
            _shadowPipe.Width = pipeBodyLength;
            _highlight.Width = pipeBodyLength - 20; // 高光稍短一些
            
            // 更新主管道位置（确保左端接口紧贴）
            Canvas.SetLeft(_mainPipe, 20);
            Canvas.SetLeft(_shadowPipe, 22);
            Canvas.SetLeft(_highlight, 30);
            
            // 更新右端接口位置（紧贴主管道右端）
            Canvas.SetLeft(_rightConnectorGrid, length - 20);
            
            // 更新接缝线位置
            Canvas.SetLeft(_seamLine1, 40); // 左接口右边缘
            Canvas.SetLeft(_seamLine2, length - 40); // 右接口左边缘
            
            // 更新容器宽度
            _pipeCanvas.Width = length;
            this.Width = length;
            
            // 更新流体指示器
            _fluidIndicator.Width = pipeBodyLength - 40;
            Canvas.SetLeft(_fluidIndicator, 40);
            Canvas.SetLeft(_flowArrow, length / 2 - 10);
        }

        private void UpdateOrientation(Orientation orientation)
        {
            var transform = new RotateTransform();
            
            if (orientation == Orientation.Vertical)
            {
                transform.Angle = 90;
                transform.CenterX = this.Width / 2;
                transform.CenterY = this.Height / 2;
            }
            
            this.RenderTransform = transform;
        }

        private void InitializeFlowAnimation()
        {
            flowAnimation = new DoubleAnimation()
            {
                From = 0.3,
                To = 0.8,
                Duration = TimeSpan.FromSeconds(1.5),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
        }

        private void StartFlowAnimation()
        {
            _fluidIndicator.BeginAnimation(OpacityProperty, flowAnimation);
        }

        private void StopFlowAnimation()
        {
            _fluidIndicator.BeginAnimation(OpacityProperty, null);
        }
    }
}
